//
//  CarSeriesVC.m
//  New_Runway
//
//  Created by mack on 2018/2/2.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarSeriesVC.h"
#import "CarVersionModel.h"

@interface CarSeriesVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
//@property (nonatomic, assign)NSInteger selIndex;//上一次选中的索引
@property (nonatomic, strong) NSMutableArray *carVersionArray;

@end

@implementation CarSeriesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"车型";
    self.view.backgroundColor= kColorWhite();
    [self initTableview];
    [self loadCarModel:self.modelId];
    
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    //    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    self.tableview.tableFooterView = [[UIView alloc] init];
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.carVersionArray.count + 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.tintColor = MAIN_NAV_COLOR();
    cell.textLabel.textColor = MAIN_FONT_COLOR();
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"不限车系";
    }else{
     versionResult *versionInfo = self.carVersionArray[indexPath.row - 1];
    cell.textLabel.text = versionInfo.modelName;
    }
    cell.textLabel.numberOfLines = 2;
    if (self.seriesIndexPath == indexPath) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.textLabel.textColor = MAIN_NAV_COLOR();
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.seriesIndexPath = indexPath;
    if (indexPath.row == 0) {
        NSDictionary *dic = @{
                              @"brandId":@(self.carBrandId),
                              @"modelId":@(self.modelId),
                              @"seriesId":@"",
                              @"brandName": self.carBrandName,
                              @"modelName": self.modelName
                              };
        NSNotification *notificatiom;
        if (self.isDesk == YES) {
            notificatiom  = [NSNotification notificationWithName:@"BrandSearchDesk" object:nil userInfo:dic];
        }else{
            notificatiom = [NSNotification notificationWithName:@"BrandSearch" object:nil userInfo:dic];
        }
        [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
        
        NSDictionary *indexoathDic = @{
                                       @"brandIndexPath":self.brandIndexPath,
                                       @"modelIndexPath":self.modelIndexPath,
                                       @"seriesIndexPath":[NSIndexPath indexPathForRow:0 inSection:0]
                                       };
        NSNotification *selectedIndexPath;
        if (self.isDesk == YES) {
            selectedIndexPath = [NSNotification notificationWithName:@"SELEINDEXPATHDESK" object:nil userInfo:indexoathDic];
        }else{
            selectedIndexPath = [NSNotification notificationWithName:@"SELEINDEXPATH" object:nil userInfo:indexoathDic];
        }

        [[NSNotificationCenter defaultCenter] postNotification:selectedIndexPath];
    }else{

    versionResult *versionInfo = self.carVersionArray[indexPath.row - 1];
    NSDictionary *dic = @{
                          @"brandId":@(self.carBrandId),
                          @"modelId":@(self.modelId),
                          @"seriesId":@(versionInfo.modelId),
                          @"brandName": self.carBrandName,
                          @"modelName": self.modelName,
                          @"seriesName": versionInfo.modelName
                          };
        NSNotification *notificatiom;
        if (self.isDesk == YES) {
            notificatiom  = [NSNotification notificationWithName:@"BrandSearchDesk" object:nil userInfo:dic];
        }else{
            notificatiom = [NSNotification notificationWithName:@"BrandSearch" object:nil userInfo:dic];
        }
        [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
        
        NSDictionary *indexoathDic = @{
                                       @"brandIndexPath":self.brandIndexPath,
                                       @"modelIndexPath":self.modelIndexPath,
                                       @"seriesIndexPath":indexPath
                                       };
        NSNotification *selectedIndexPath;
        if (self.isDesk == YES) {
            selectedIndexPath = [NSNotification notificationWithName:@"SELEINDEXPATHDESK" object:nil userInfo:indexoathDic];
        }else{
            selectedIndexPath = [NSNotification notificationWithName:@"SELEINDEXPATH" object:nil userInfo:indexoathDic];
        }

        [[NSNotificationCenter defaultCenter] postNotification:selectedIndexPath];
        
        
    }
    if (self.isDesk == YES) {
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
        
    } else {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
    [self.tableview reloadData];
    
}

//获取车型
-(void)loadCarModel:(NSInteger)seriesId{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,CarModelApi];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:@{@"seriesId":@(seriesId)} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        CarVersionModel *model = [CarVersionModel mj_objectWithKeyValues:response];
        if (model.code == 1) {
            [self.carVersionArray removeAllObjects];
            for (NSDictionary *dic in model.result) {
                versionResult *versionInfo = [versionResult mj_objectWithKeyValues:dic];
                [self.carVersionArray addObject:versionInfo];
            }
            
        }
        [self.tableview reloadData];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
}
-(NSMutableArray *)carVersionArray{
    if (!_carVersionArray) {
        _carVersionArray = [[NSMutableArray alloc] init];
    }
    return _carVersionArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
